package com.pl.lister;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.pl.entity.Device;
import com.pl.entity.Devicefl;
import com.pl.service.DeviceService;
import com.pl.service.DeviceflService;

import java.util.ArrayList;
import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author YanHaoLong
 * @date 2024/5/5
 * <p>
 * -- 代码敲烂 月薪过万--
 */
public class deviceflReadLister extends AnalysisEventListener<Devicefl> {
    //定义初始加入的记录数
    private static final int BATCH_COUNT=5;
    //引入SysUserService,后边导入会用到
    private DeviceflService deviceflService;
    //创建初始集合存sysUser数据
    List<Devicefl> deviceflList =new ArrayList<>();

    public deviceflReadLister() {
    }
    public deviceflReadLister(DeviceflService deviceflService) {
        this.deviceflService = deviceflService;
    }


    @Override
    public void invoke(Devicefl devicefl, AnalysisContext analysisContext) {
        deviceflList.add(devicefl);
        if (deviceflList.size()>=BATCH_COUNT){
            saveDate();
            deviceflList.clear();
        }
    }
    private void saveDate() {
        System.out.println("开始批量导入");
        deviceflService.inserts(deviceflList);
        System.out.println("结束批量导入");
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (deviceflList.size()>0){  //最后一次存的地方
            saveDate();
            deviceflList.clear();
        }
    }
}
